package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.print.DocFlavor;
import java.util.List;

/***
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 */

@RestController
@RequestMapping("menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = menuService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, "分页查询菜单列表成功", pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "分页查询菜单列表失败");
        }
    }

    @RequestMapping("findMenuAll")
    public Result findMenuAll(){
        try {
            List<Menu> menuList = menuService.findMenuAll();
            return new Result(true,"查询菜单列表成功",menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询菜单列表失败");
        }
    }

    @RequestMapping("addMenu")
    public Result addMenu(@RequestBody Menu menu) {
        try {
            menuService.addMenu(menu);
            return new Result(true, "添加菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加菜单失败");
        }
    }



    @RequestMapping("findMenuByRoleId")
    public Result findMenuByRoleId(@RequestParam("id") Integer id){
        try {
           List<Integer> ids = menuService.findRoleAndMenuByRoleId(id);
            return new Result(true,"查询角色引用菜单成功",ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色引用菜单失败");
        }
    }


    @RequestMapping("deleteMenuById")
    public Result deleteMenu(@RequestParam("id") Integer id) {
        try {
            menuService.deleteMenuById(id);
            return new Result(true, "删除菜单成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除菜单失败");
        }
    }

    @RequestMapping("findMenuById")
    public Result findMenuById(@RequestParam("id") Integer id) {
        try {
            Menu menu = menuService.findMenuById(id);
            return new Result(true, "查询菜单success", menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询菜单error");
        }
    }

    @RequestMapping("updateMenu")
    public Result updateMenu(@RequestBody Menu menu){
        try {
        menuService.updateMenu(menu);
        return new Result(true, "更新数据成功", menu);
    } catch (Exception e) {
        e.printStackTrace();
        return new Result(false, "更新数据失败");
    }
    }
}

/***
 *                    .::::.
 *                  .::::::::.
 *                 ::::::::::: 
 *             ..:::::::::::'
 *           '::::::::::::'
 *             .::::::::::
 *        '::::::::::::::..
 *             ..::::::::::::.
 *           ``::::::::::::::::
 *            ::::``:::::::::'        .:::.
 *           ::::'   ':::::'       .::::::::.
 *         .::::'      ::::     .:::::::'::::.
 *        .:::'       :::::  .:::::::::' ':::::.
 *       .::'        :::::.:::::::::'      ':::::.
 *      .::'         ::::::::::::::'         ``::::.
 *  ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 *                    '.:::::'                    ':'````..
 */